using System;
using System.IO;
using System.Windows.Forms;
using ConfigurationSettings;
using ServiceLocator;

namespace FileTracker.Forms
{
    public partial class FormAddFavorite : Form
    {
        private string _CurrentPath;

        public string CurrentPath
        {
            get { return _CurrentPath; }
            set { _CurrentPath = value; }
        }

        public FormAddFavorite()
        {
            InitializeComponent();
        }
        
        private void buttonOK_Click(object sender, EventArgs argEventArgs)
        {
            //ApplicationSettings ConfigFile;
            string FavoritePath = textBoxPath.Text;
            string FavoriteName = textBoxName.Text;

            //Check for path exists
            if (!Directory.Exists(FavoritePath))
            {
                MessageBox.Show("That path does not exist.  Please try again.", "Invalid MenuItemFavoriteLeft path");
                textBoxPath.Focus();
                return;
            }

            //Load the config file
            //ConfigFile = new ApplicationSettings();
	        IApplicationSettings localSettings = Locator.Resolve<IApplicationSettings>();

            //Check the current favorites for duplicate names.
			if (localSettings.FavoriteList.ContainsKey(FavoriteName))
            {
                MessageBox.Show("There is already a MenuItemFavoriteLeft with that name.  Please try again.", "Invalid MenuItemFavoriteLeft name");
                return;
            }

            //Check the current favorites for duplicate locations
			if (localSettings.FavoriteList.ContainsValue(FavoritePath))
            {
                MessageBox.Show("There is already a MenuItemFavoriteLeft with that path.  Please try again.", "Invalid MenuItemFavoriteLeft path");
                return;
            }

            //Format 'location' properly
            if (FavoritePath.EndsWith(@"\"))
                FavoritePath = FavoritePath.Substring(0, FavoritePath.Length - 1);

			localSettings.FavoriteList.Add(FavoriteName, FavoritePath);

			localSettings.Save();

            //Close this window.
            this.Close();
        }

        private void buttonPathBrowseDirectoryLeft_Click(object sender, EventArgs argEventArgs)
        {
            FolderBrowserDialog BrowseDefault;

            //Open directory browser
            BrowseDefault = new FolderBrowserDialog();
            BrowseDefault.ShowNewFolderButton = false;
            BrowseDefault.RootFolder = Environment.SpecialFolder.DesktopDirectory;

            BrowseDefault.ShowDialog(this);
            textBoxPath.Text = BrowseDefault.SelectedPath;

        }

        private void FormAddFavorite_Shown(object sender, EventArgs argEventArgs)
        {
            textBoxPath.Text = _CurrentPath;
        }

    }
}